package luanvan.dragdrop;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class SitesOverlay extends ItemizedOverlay {
	private List<OverlayItem> items = new ArrayList<OverlayItem>();
	private Drawable marker = null;
	private OverlayItem inDrag = null;
	private ImageView dragImage = null;
	private int xDragImageOffset = 0;
	private int yDragImageOffset = 0;
	private int xDragTouchOffset = 0;
	private int yDragTouchOffset = 0;
	MapView map;

	public SitesOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy,
			Drawable marker, ImageView dragImage, MapView map) {
		super(pDefaultMarker, pResourceProxy);
		this.marker = marker;
		this.dragImage = dragImage;
		this.map = map;

		xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
		yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();

		items.add(new OverlayItem("", "", getPoint(10.78,
				106.69)));
		
		populate();
	}

	private GeoPoint getPoint(double lat, double lon) {
		return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
	}

	@Override
	public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected OverlayItem createItem(int arg0) {
		// TODO Auto-generated method stub
		return items.get(arg0);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		final int action = event.getAction();
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		boolean result = false;
		if(action==MotionEvent.ACTION_DOWN){
			for (OverlayItem item : items) {
				Point p=new Point(0 , 0);
				map.getProjection().toPixels(item.getPoint(), p);
				if(hitTest(item, marker, x-p.x, y-p.y)){
					result=true;
					inDrag =item;
					items.remove(inDrag);
					populate();
					
					xDragTouchOffset=0;
					yDragTouchOffset=0;
					setDragImagePosition(p.x, p.y);
					dragImage.setVisibility(View.VISIBLE);
					
					xDragTouchOffset=x-p.x;
					yDragTouchOffset=y-p.y;
					break;
				}
			}
		}else if(action==MotionEvent.ACTION_MOVE && inDrag!=null){
			setDragImagePosition(x, y);
			result=true;
		}else if(action==MotionEvent.ACTION_UP && inDrag!=null){
			dragImage.setVisibility(View.GONE);
			GeoPoint pt=(GeoPoint) map.getProjection().fromPixels(x-xDragTouchOffset, y-yDragTouchOffset);
			OverlayItem toDrop=new OverlayItem("", "", pt);
			items.add(toDrop);
			populate();
			inDrag=null;
			result=true;
		}
		
		
		
		return result|| super.onTouchEvent(event, mapView);
	}

	@Override
	protected void draw(Canvas canvas, MapView mapview, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapview, shadow);
		boundToHotspot(marker, null);
	}

	private void setDragImagePosition(int x, int y) {
		RelativeLayout.LayoutParams lp = (LayoutParams) dragImage
				.getLayoutParams();
		lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
				- yDragImageOffset - yDragTouchOffset, 0, 0);
		dragImage.setLayoutParams(lp);
	}

}
